package com.springBoot.springBootSysCore.modules.entity.system;


import java.util.List;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.springBoot.springBootSysCore.common.comment.annotation.MyComment;
import com.springBoot.springBootSysCore.common.persistence.DataEntity;
/**
 * 资源表
 */
@Entity
@Table(name="SYS_RESOURCE")
@MyComment("资源表")
public class SysResource  extends DataEntity<SysResource>{
	private static final long serialVersionUID = 1L;
	private String name;//名称
	private String url;//资源地址
	private String remark;//资源描述
	private String code;
	private String iconClass;//图标
	private Integer isButton;//判断是否是按钮
	private SysResource parentResource;//父资源
	private List<SysResource> childrenResource;//子项
	private List<SysRole> roles;//角色

	
	@Column(name="NAME",length=255,nullable=false)
	@MyComment("资源名称")
	@NotNull(message="资源名称不能为空")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(name="URL",length=256)
	@MyComment("资源对应的URL")
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	@Column(name="REMARK",length=256)
	@MyComment("资源描述")
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	@Column(name="CODE")
	@MyComment("资源CODE")
	@NotNull(message="资源code不能为空")
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	@Column(name="IS_BUTTON",columnDefinition="INT default 0")
	@MyComment("是否是按钮")
	public Integer getIsButton() {
		return isButton;
	}
	public void setIsButton(Integer isButton) {
		this.isButton = isButton;
	}
	@Column(name="ICON_CLASS")
	@MyComment("菜单显示的图标")
	public String getIconClass() {
		return iconClass;
	}
	public void setIconClass(String iconClass) {
		this.iconClass = iconClass;
	}
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="P_ID")
	@MyComment("本资源的父资源ID")
	public SysResource getParentResource() {
		return parentResource;
	}
	public void setParentResource(SysResource parentResource) {
		this.parentResource = parentResource;
	}
	@OneToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST},mappedBy = "parentResource")
	@Fetch(FetchMode.SUBSELECT)
	@JsonIgnore
	public List<SysResource> getChildrenResource() {
		return childrenResource;
	}
	public void setChildrenResource(List<SysResource> childrenResource) {
		this.childrenResource = childrenResource;
	}
	@ManyToMany(mappedBy="resources",cascade=CascadeType.ALL)
	@JsonIgnore
	public List<SysRole> getRoles() {
		return roles;
	}
	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}

	@JsonIgnore
	@Transient
	public static String getRootId(){
		return "1";
	}
}
